package com.apache.funds.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.funds.entity.SvmFlowing;
import com.apache.funds.manager.SvmFlowingManager;

public class SvmFlowingManagerImpl implements SvmFlowingManager {

	protected IDao svmFlowingDao;
	protected final String entityName = "com.apache.funds.entity.SvmFlowing";

	public void setSvmFlowingDao(IDao svmFlowingDao) {
		this.svmFlowingDao = svmFlowingDao;
	}

	public String saveInfo(ParamsVo<SvmFlowing> vo) throws BusinessException {
		SvmFlowing sflowing = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="sptsTransInfoDetail_"+infoId;
		sflowing.setId(infoId);
		MethodParam param = new MethodParam("SvmFlowing", cacheKey, "", entityName);
		param.setVaule(sflowing);
		if (svmFlowingDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<SvmFlowing> vo) throws BusinessException {
		SvmFlowing sptsTransInfoDetail = vo.getObj();
		if (Validator.isNotNull(sptsTransInfoDetail.getId())) {
			String cacheKey = "";
			//String cacheKey="sptsTransInfoDetail_"+sptsTransInfoDetail.getId();
			MethodParam param = new MethodParam("SvmFlowing", cacheKey, "", entityName);
			param.setVaule(sptsTransInfoDetail);
			return svmFlowingDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<SvmFlowing> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="sptsTransInfoDetail_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		SvmFlowing info = (SvmFlowing) svmFlowingDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("SptsTransInfoDetail");
			return svmFlowingDao.edit(param);
		} else {
			param.setParams("id", infoId);
			param.setDelete(true);
			return svmFlowingDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<SvmFlowing> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="sptsTransInfoDetail_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return svmFlowingDao.selectById(param);
	}

	public Object execute(ParamsVo<SvmFlowing> vo) {

		return null;
	}

	public Page getPageInfo(ParamsVo<SvmFlowing> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")), "");
		if (Validator.isNotNull(orderBy)) {
			param.setOrderby(orderBy);
		}
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = svmFlowingDao.pageSelect(param);
		return page;
	}

	public List<SvmFlowing> getList(ParamsVo<SvmFlowing> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")), "");
		if (Validator.isNotNull(orderBy)) {
			param.setOrderby(orderBy);
		}
		return svmFlowingDao.select(param);
	}

	public long countInfo(ParamsVo<SvmFlowing> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return svmFlowingDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<SvmFlowing> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(svmFlowingDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		SvmFlowing svmFlowing = vo.getObj();
		if (Validator.isNotNull(svmFlowing.getTransDate())) {
			sb.append(" and transDate = :transDate");
			param.setParams("transDate", svmFlowing.getTransDate());
		}
		if (Validator.isNotNull(svmFlowing.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", svmFlowing.getUserId());
		}
		if (Validator.isNotNull(svmFlowing.getDirection())) {
			sb.append(" and direction = :direction");
			param.setParams("direction", svmFlowing.getDirection());
		}
		if (Validator.isNotNull(svmFlowing.getAmount())) {
			sb.append(" and amount = :amount");
			param.setParams("amount", svmFlowing.getAmount());
		}
		if (Validator.isNotNull(svmFlowing.getActionType())) {
			sb.append(" and actionType = :actionType");
			param.setParams("actionType", svmFlowing.getActionType());
		}
		if (Validator.isNotNull(svmFlowing.getActionId())) {
			sb.append(" and actionId = :actionId");
			param.setParams("actionId", svmFlowing.getActionId());
		}
		if (Validator.isNotNull(svmFlowing.getActionName())) {
			sb.append(" and actionName = :actionName");
			param.setParams("actionName", svmFlowing.getActionName());
		}
		if (Validator.isNotNull(svmFlowing.getBankType())) {
			sb.append(" and bankType = :bankType");
			param.setParams("bankType", svmFlowing.getBankType());
		}
		if (Validator.isNotNull(svmFlowing.getStartTime())) {
			sb.append(" and startTime = :startTime");
			param.setParams("startTime", svmFlowing.getStartTime());
		}
		if (Validator.isNotNull(svmFlowing.getEndTime())) {
			sb.append(" and endTime = :endTime");
			param.setParams("endTime", svmFlowing.getEndTime());
		}
		if (Validator.isNotNull(svmFlowing.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", svmFlowing.getCreateTime());
		}
		if (Validator.isNotNull(svmFlowing.getStatus())) {
			sb.append(" and status = :status");
			param.setParams("status", svmFlowing.getStatus());
		}
		if (Validator.isNotNull(svmFlowing.getBankFlowId())) {
			sb.append(" and bankFlowId = :bankFlowId");
			param.setParams("bankFlowId", svmFlowing.getBankFlowId());
		}
		if (Validator.isNotNull(svmFlowing.getAcctId())) {
			sb.append(" and acctId = :acctId");
			param.setParams("acctId", svmFlowing.getAcctId());
		}
		if (Validator.isNotNull(svmFlowing.getOpacctId())) {
			sb.append(" and opacctId = :opacctId");
			param.setParams("opacctId", svmFlowing.getOpacctId());
		}
		if (Validator.isNotNull(svmFlowing.getMoneyType())) {
			sb.append(" and moneyType = :moneyType");
			param.setParams("moneyType", svmFlowing.getMoneyType());
		}
		if (Validator.isNotNull(svmFlowing.getTxtTime())) {
			sb.append(" and txtTime = :txtTime");
			param.setParams("txtTime", svmFlowing.getTxtTime());
		}
		if (Validator.isNotNull(svmFlowing.getSignNo())) {
			sb.append(" and signNo = :signNo");
			param.setParams("signNo", svmFlowing.getSignNo());
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams().get("actype")))) {
			param.setParams("actype", (String) vo.getParams().get("actype"));
		}
		param.setSqlStr(sb.toString());
		return param;
	}

}
